#include <unordered_set>
#include <iostream>

using namespace std;

class Solution {
    int cal_square(int num) {
        int out = 0;
        do {
            int val = num % 10;
            out += val * val;
            num /= 10;
        } while (num != 0);
        return out;
    }
public:
    bool isHappy(int n) {
        if (n == 1) {
            return true;
        }
        unordered_set<int> his;
        his.insert(n);
        while (true) {
            int next_val = cal_square(n);
            if (his.count(next_val) == 1) {
                return false;
            } else if (next_val == 1) {
                return true;
            }
            his.insert(next_val);
            n = next_val;
        }
    }
};

int main() {
    Solution solution;
    int output = solution.isHappy(19);
    cout << output << endl;
}